{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Custom filters and other infrastructure types\n",
    "\n",
    "Author: [Geoff Boeing](https://geoffboeing.com/)\n",
    "\n",
    "Use OSMnx to download and visualize a power line network and a subway system.\n",
    "\n",
    "  - [Documentation](https://osmnx.readthedocs.io/)\n",
    "  - [Journal article and citation info](https://geoffboeing.com/publications/osmnx-paper/)\n",
    "  - [Code repository](https://github.com/gboeing/osmnx)\n",
    "  - [Examples gallery](https://github.com/gboeing/osmnx-examples)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import osmnx as ox\n",
    "\n",
    "ox.settings.log_console = True\n",
    "ox.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use custom filters to fine-tune your network. OSMnx uses `network_type` presets to query for streets that allow walking, biking, driving, etc. You can override this by passing a `custom_filter` to specify specific OSM ways you want in your graph."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "place = {\"city\": \"Berkeley\", \"state\": \"California\"}\n",
    "\n",
    "# only get motorway ways\n",
    "cf = '[\"highway\"~\"motorway\"]'\n",
    "G = ox.graph_from_place(place, network_type=\"drive\", custom_filter=cf)\n",
    "print(len(G), \"motorway\")\n",
    "\n",
    "# only get primary ways\n",
    "cf = '[\"highway\"~\"primary\"]'\n",
    "G = ox.graph_from_place(place, network_type=\"drive\", custom_filter=cf)\n",
    "print(len(G), \"primary\")\n",
    "\n",
    "# use the pipe (|) as 'or' operator\n",
    "cf = '[\"highway\"~\"motorway|primary\"]'\n",
    "G = ox.graph_from_place(place, network_type=\"drive\", custom_filter=cf)\n",
    "print(len(G), \"motorway + primary\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# network of the canals of amsterdam\n",
    "place = \"Amsterdam, Netherlands\"\n",
    "G = ox.graph_from_place(place, custom_filter='[\"waterway\"~\"canal\"]')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To download the road network for an entire country, you often need to limit your query to something like motorways only, to fit in your computer's RAM. For large queries, such as the entire nation of Belgium, OSMnx will subdivide your query into multiple server requests to download all the data, then assemble the graph."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "# get only motorways, trunks, and their links in all of Belgium\n",
    "# takes a couple minutes to do all the downloading and processing\n",
    "# OSMnx automatically divides up the query into multiple requests to not overload server\n",
    "cf = '[\"highway\"~\"motorway|motorway_link|trunk|trunk_link\"]'\n",
    "G = ox.graph_from_place(\"Belgium\", network_type=\"drive\", custom_filter=cf)\n",
    "fig, ax = ox.plot_graph(G, node_size=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get NY subway rail network\n",
    "# note this is rail *infrastructure* and thus includes crossovers, sidings, spurs, yards, etc\n",
    "# for station-based rail network, you should download a station adjacency matrix elsewhere\n",
    "ox.settings.useful_tags_way += [\"railway\"]\n",
    "G = ox.graph_from_place(\n",
    "    \"New York, New York, USA\",\n",
    "    retain_all=False,\n",
    "    truncate_by_edge=True,\n",
    "    simplify=True,\n",
    "    custom_filter='[\"railway\"~\"subway\"]',\n",
    ")\n",
    "\n",
    "fig, ax = ox.plot_graph(G, node_size=0, edge_color=\"w\", edge_linewidth=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (ox)",
   "language": "python",
   "name": "ox"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
